package com.auto.video.tv.adapter;

import android.content.Context;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.auto.video.tv.R;
import com.auto.video.tv.bean.WeatherInfo;
import com.xin.auto.lib.base.CustomTextWatcher;

import java.util.List;

public class WeatherAdapter extends RecyclerView.Adapter<WeatherAdapter.WeatherViewHolder> {
    private Context mContext;
    private List<WeatherInfo> mDataList;
    private WeatherViewHolder mHolder;

    public WeatherAdapter(Context context) {
        this.mContext = context;
    }

    public void setDataList(List<WeatherInfo> dataList) {
        if (this.mDataList != null && mDataList.size() > 0) {
            this.mDataList.clear();
        }
        this.mDataList = dataList;
    }

    @NonNull
    @Override
    public WeatherViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.app_weather_adapter_item, parent, false);
        mHolder = new WeatherViewHolder(view);
        return mHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull WeatherViewHolder holder, int position) {
        WeatherInfo weatherInfo = mDataList.get(position);
        holder.vWeatherTitle.setText(weatherInfo.getTitle());
        holder.vWeatherSubTitle.setText(weatherInfo.getSubTitle());
        holder.vWeatherUnit.setText(weatherInfo.getUnit());
        holder.vWeatherValue.setText(weatherInfo.getValue());
        mHolder.vWeatherValue.addTextChangedListener(new CustomTextWatcher() {
            @Override
            public void afterTextChanged(Editable editable) {
                weatherInfo.setValue(editable.toString());
            }
        });
        holder.vTagBg.setBackgroundResource(weatherInfo.getBackground());
    }

    @Override
    public int getItemCount() {
        return mDataList != null ? mDataList.size() : 0;
    }

    public void setEditMode(boolean focusAble) {
//        mHolder.vWeatherValue.setFocusable(focusAble);
//        mHolder.vWeatherValue.setFocusableInTouchMode(focusAble);
    }

    protected static class WeatherViewHolder extends RecyclerView.ViewHolder {
        TextView vWeatherTitle;
        TextView vWeatherSubTitle;
        TextView vWeatherUnit;
        EditText vWeatherValue;
        LinearLayout vTagBg;

        public WeatherViewHolder(@NonNull View itemView) {
            super(itemView);
            vTagBg = itemView.findViewById(R.id.tag_background);
            vWeatherTitle = itemView.findViewById(R.id.weather_title);
            vWeatherSubTitle = itemView.findViewById(R.id.sub_title);
            vWeatherUnit = itemView.findViewById(R.id.weather_unit);
            vWeatherValue = itemView.findViewById(R.id.weather_value);
        }
    }
}
